package com.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iot.enums.StatusEnum;
import com.iot.mapper.AuthInfoMapper;
import com.iot.mapper.UserInfoMapper;
import com.iot.model.entity.AuthInfoEntity;
import com.iot.model.entity.UserInfoEntity;
import com.iot.model.vo.AuthInfoVo;
import com.iot.service.AuthInfoService;
import com.iot.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * AuthInfoServiceImpl
 *
 * @author: dh
 * @date: 2023-11-10 10:59:35
 */
@Service
@RequiredArgsConstructor
public class AuthInfoServiceImpl implements AuthInfoService {

    private final AuthInfoMapper authInfoMapper;

    private final UserInfoMapper userInfoMapper;

    @Override
    public AuthInfoVo getByUsername(String username) {
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        LambdaQueryWrapper<AuthInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AuthInfoEntity::getUsername, username);
        queryWrapper.eq(AuthInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(AuthInfoEntity::getCreateTime);
        queryWrapper.last("limit 1");
        AuthInfoEntity authInfoEntity = authInfoMapper.selectOne(queryWrapper);
        if (null == authInfoEntity) {
            return null;
        }
        AuthInfoVo vo = new AuthInfoVo();
        vo.setId(authInfoEntity.getId());
        vo.setUsername(authInfoEntity.getUsername());
        return vo;
    }

    @Override
    public AuthInfoVo add(String username, Integer language) {
        AuthInfoEntity entity = new AuthInfoEntity();
        entity.setUsername(username);
        //entity.setPassword(authInfo.getPassword());
        entity.setState(StatusEnum.VALID.getKey());
        entity.setCreateTime(DateUtil.now());
        authInfoMapper.insert(entity);

        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setAuthId(entity.getId());
        userInfoEntity.setPhone(username);
        userInfoEntity.setLanguage(language);
        userInfoEntity.setIsOn(0);
        userInfoEntity.setState(StatusEnum.VALID.getKey());
        userInfoEntity.setCreateTime(DateUtil.now());
        userInfoMapper.insert(userInfoEntity);
        AuthInfoVo vo = new AuthInfoVo();
        vo.setId(entity.getId());
        vo.setUid(userInfoEntity.getId());
        return vo;
    }

    @Override
    public AuthInfoVo applyAdd(String username, Integer language,String applyEmail) {
        AuthInfoEntity entity = new AuthInfoEntity();
        entity.setUsername(username);
        //entity.setPassword(authInfo.getPassword());
        entity.setState(StatusEnum.VALID.getKey());
        entity.setCreateTime(DateUtil.now());
        authInfoMapper.insert(entity);

        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setAuthId(entity.getId());
        userInfoEntity.setLanguage(language);
        userInfoEntity.setIsOn(0);
        userInfoEntity.setApplyEmail(applyEmail);
        userInfoEntity.setState(StatusEnum.VALID.getKey());
        userInfoEntity.setCreateTime(DateUtil.now());
        userInfoMapper.insert(userInfoEntity);
        AuthInfoVo vo = new AuthInfoVo();
        vo.setId(entity.getId());
        vo.setUid(userInfoEntity.getId());
        return vo;
    }

    @Override
    public AuthInfoEntity getByAuthId(Long authId) {
        if (null == authId) {
            return null;
        }
        LambdaQueryWrapper<AuthInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AuthInfoEntity::getId, authId);
        queryWrapper.eq(AuthInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(AuthInfoEntity::getCreateTime);
        queryWrapper.last("limit 1");
        return authInfoMapper.selectOne(queryWrapper);
    }

}
